package com.dycx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.user.converter.VehicleTypeSeatConverter;
import com.dycx.user.dto.VehicleTypeSeatDTO;
import com.dycx.user.entity.VehicleTypeSeatDO;
import com.dycx.user.mapper.VehicleTypeSeatMapper;
import com.dycx.user.service.VehicleTypeSeatService;
import com.dycx.user.vo.VehicleTypeSeatVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class VehicleTypeSeatServiceImpl extends ServiceImpl<VehicleTypeSeatMapper, VehicleTypeSeatDO> implements VehicleTypeSeatService {

    private final VehicleTypeSeatMapper vehicleTypeSeatMapper;


    @Override
    public List<VehicleTypeSeatVO> getList(Integer vehicleTypeId) {
        LambdaQueryWrapper<VehicleTypeSeatDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VehicleTypeSeatDO::getVehicleTypeId, vehicleTypeId);
        queryWrapper.last(" order by row asc,col asc ");

        return VehicleTypeSeatConverter.entityToVoList(this.list(queryWrapper));
    }

    @Override
    public Boolean delete(Integer id) {
        return this.removeById(id);
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean insert(VehicleTypeSeatDTO dto) {
        if (this.getInfoByVehicleTypeSeatName(dto.getSeatName(), dto.getVehicleTypeId()) != null) {
            throw new RuntimeException("已存在相同车型名称，请勿重复添加");
        }
        return this.save(VehicleTypeSeatConverter.dtoToEntity(dto));
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean update(VehicleTypeSeatDTO dto) {
        return this.updateById(VehicleTypeSeatConverter.dtoToEntity(dto));
    }

    /**
     * 更新排序
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean updateSort(VehicleTypeSeatDTO dto) {
        VehicleTypeSeatDO carTypeSeat = new VehicleTypeSeatDO();
        carTypeSeat.setId(dto.getId());
        carTypeSeat.setSort(dto.getSort());
        return this.updateById(VehicleTypeSeatConverter.dtoToEntity(dto));
    }

    /**
     * 更新状态
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean updateStatus(VehicleTypeSeatDTO dto) {
        VehicleTypeSeatDO carTypeSeat = new VehicleTypeSeatDO();
        carTypeSeat.setId(dto.getId());
        carTypeSeat.setStatus(dto.getStatus());
        return this.updateById(carTypeSeat);
    }

    @Override
    public VehicleTypeSeatVO getInfoById(Integer id) {
        return vehicleTypeSeatMapper.getInfoById(id);
    }

    @Override
    public VehicleTypeSeatVO getInfoByVehicleTypeSeatName(String seatName, Integer vehicleTypeId) {
        return vehicleTypeSeatMapper.getInfoByCarTypeSeatName(seatName, vehicleTypeId);
    }
}
